import com.pi4j.wiringpi.Gpio;
import com.pi4j.wiringpi.SoftPwm;

public class LedClient {
    private int pinRed ;
    private int pinGreen ;
    private int pinBlue ;
    public boolean isRunning = false ;


    public LedClient( ) {
        pinRed  = 0 ;
        pinGreen=1 ;
        pinBlue=2 ;
    }

    public LedClient(int pinRed, int pinGreen, int pinBlue) {
        this.pinRed = pinRed;
        this.pinGreen = pinGreen;
        this.pinBlue = pinBlue;
    }

    private void initLed(){
        Gpio.wiringPiSetup(); // 初始化
        SoftPwm.softPwmCreate(pinRed, 0, 100);
        SoftPwm.softPwmCreate(pinGreen, 0, 100);
        SoftPwm.softPwmCreate(pinBlue, 0, 100);
    }

    public   void openLed(int color){
        initLed();
        ledColorSet( color );// openLed
    }

    public   void closeLed(){
        isRunning =false ;

        initLed();

        SoftPwm.softPwmStop(pinRed);
        SoftPwm.softPwmStop(pinGreen);
        SoftPwm.softPwmStop(pinBlue);
    }

    private  void ledColorSet(int color  )       {
        int r_val = (color & 0xFF0000) >> 16;  //get red value
        int g_val = (color & 0x00FF00) >> 8;   //get green value
        int b_val = (color & 0x0000FF)   ;   //get blue value  (color & 0x0000FF) >> 0

        r_val = map(r_val, 0, 255, 0, 100);    //change a num(0~255) to 0~100
        g_val = map(g_val, 0, 255, 0, 100);
        b_val = map(b_val, 0, 255, 0, 100);

        SoftPwm.softPwmWrite(pinRed,     r_val);
        SoftPwm.softPwmWrite(pinGreen,   g_val);
        SoftPwm.softPwmWrite(pinBlue,    b_val);
    }




    public  void ledBreath( final int ledColor , final int time1 , final int time2  ){

        new Thread(new Runnable() {
            @Override
            public void run() {
                initLed();

                int r_val = (ledColor & 0xFF0000) >> 16;  //get red value
                int g_val = (ledColor & 0x00FF00) >> 8;   //get green value
                int b_val = (ledColor & 0x0000FF)   ;   //get blue value  (color & 0x0000FF) >> 0

                r_val = map(r_val, 0, 255, 0, 100);
                g_val = map(g_val, 0, 255, 0, 100);
                b_val = map(b_val, 0, 255, 0, 100);

                while (isRunning){
                    // 变亮
                    for(int i=0 ;i< 10 ;i++){
                        SoftPwm.softPwmWrite(pinRed,     r_val/10*i);
                        SoftPwm.softPwmWrite(pinGreen,    g_val/10*i);
                        SoftPwm.softPwmWrite(pinBlue,     b_val/10*i);
                        try {
                            Thread.sleep(time1);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }

                    //变暗
                    for(int i=10 ;i>0 ;i--){
                        SoftPwm.softPwmWrite(pinRed,     r_val/10*i);
                        SoftPwm.softPwmWrite(pinGreen,    g_val/10*i);
                        SoftPwm.softPwmWrite(pinBlue,     b_val/10*i);
                        try {
                            Thread.sleep(time2);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

    }


    public   void ledFlicker( final int [] ledColors ,final int sleepTime ){

        new Thread(new Runnable() {
            @Override
            public void run() {
                initLed();
                // 赤橙黄绿青蓝紫
                int []colors = {
                        0xFF0000,   0xFF7F00,  0xFFFF00 ,  0x00FF00,
                        0x00FFFF,  0x0000FF,  0x8B00FF ,  0xFFFFFF
                } ;
                while (isRunning){
                    for(int color :ledColors){
                        ledColorSet(color ); //ledFlicker
                        try {
                            Thread.sleep(sleepTime);
                        } catch (InterruptedException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }).start();

    }


    /**
     * 将一个数从一个区间线性映射到另一个区间，比如将0~100之间的一个数映射到0~255之间
     */
    private  int map(int x, int in_min, int in_max, int out_min, int out_max) {
        return (x - in_min) * (out_max - out_min) / (in_max - in_min) + out_min;
    }
}
